﻿using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    private Transform targetTrans;
    private bool isMove = false;
    private bool isMoveX = false;
    private bool isMoveY = false;

    public void MoveTo(Coord target)
    {
        targetTrans = target.gameObject.transform;
        isMove = true;
        if (Mathf.Abs(transform.position.x - gameObject.transform.position.x) > Mathf.Abs(transform.position.x - gameObject.transform.position.x))
        {
            isMoveY = true;
        }
        else
        {
            isMoveX = true;
        }
    }

    private void Start()
    {
        transform.position = new Vector3(transform.position.x, MapManager.height, transform.position.z);
    }

    private void Update()
    {
        if (isMove)
        {
            if (isMoveX)
            {
                gameObject.transform.position = Vector3.MoveTowards(transform.position, new Vector3(targetTrans.position.x,transform.position.y,transform.position.z), 0.5f);
                if (transform.position.x == targetTrans.position.x)
                {
                    isMoveX = false;
                    isMoveY = true;
                }
            }

            if (isMoveY)
            {
                gameObject.transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x, transform.position.y, targetTrans.position.z), 0.5f);
                if (transform.position.z == targetTrans.position.z)
                {
                    isMoveX = false;
                    isMoveY = true;
                }
            }

            if (gameObject.transform.position == targetTrans.position)
            {
                isMove = false;
                isMoveX = false;
                isMoveY = false;
            }
        }
        
    }
}
